<?php

require_once("../../core/header.php");
require_once("../../core/cfg.php");

class ajax_get_groups
{
	public static function get($parent = 1, $lvl = 0, $id, $pid)
	{
		global $lvl, $var;
		
		$lvl++;	
		for($i=0;$i<$lvl;$i++){
			for($j=0;$j<$i;$j++) {
				$mdash.= '&nbsp;';
			}	
		}
		if($parent == 0) {
			$var.= '<option value="0"'.(($pid == 0) ? ' selected="selected" style="font-weight:bold;"' : '').'>'.$mdash.' Корень каталога</option>';
		}
		
		if(!empty($mdash)) {
			$mdash.= '| &mdash;';
		}
		
		$r =& Registry::get("MDB")->query("SELECT * FROM `".$_POST["table"]."` WHERE `parent`='".$parent."' ORDER BY `por`;");		
		if (PEAR::isError($r)) {
			die($r->getMessage().' ::: '.$r->getUserInfo());
		}		
		if ($r->numRows()) 
		{
			while($p = $r->fetchRow(MDB2_FETCHMODE_ASSOC)) 
			{
				if($id == $p['id']) {
					$_selected = ' disabled="disabled" style="font-weight:bold;"';
				}
				elseif($pid == $p['id']) {
					$_selected = ' selected="selected"';
				}
				else {
					$_selected = '';
				}
										 
				$var.= '<option value="'.$p['id'].'"'.$_selected.'>'.$mdash.' '.$p['name'].'</option>';
				
				ajax_get_groups::get($p["id"], $lvl, $id, $pid);
				$lvl--;
			}
		}		
		return $var;
	}	
}

exit(ajax_get_groups::get(1, 0, $_POST["parent"], $_POST["curr_id"]));

?>